//
//  EFCircleViewController.m
//  insurance
//
//  Created by 王长飞 on 2019/9/26.
//  Copyright © 2019年 王长飞. All rights reserved.
//

#import "MoreGoodsController.h"
#import "WKWebViewController.h"
#import "GoodsDetailController.h"
#import "MoreGoodsCell.h"
#import "MJRefresh.h"
#import "MoreGoodsModel.h"

@interface MoreGoodsController ()<UITableViewDataSource,UITableViewDelegate,BaseModelProtocol,UITextFieldDelegate>

@property (nonatomic,strong)UIView *searchBgView ;
@property (nonatomic,strong)UITextField *searchField;
@property (nonatomic,strong)UIView *selectView ;
@property (nonatomic,strong)UITableView *tableView ;

@property (nonatomic,strong)MoreGoodsModel *dataModel ;
@property (nonatomic,assign)int pageIndex ;
@property (nonatomic,strong)NSMutableArray *dataArray ;

@property (nonatomic,assign)int selectIndex ;
@property (nonatomic,strong)UIView *selectBottomline;
@property (nonatomic,assign)int salesNumSort;
@property (nonatomic,assign)int priceSort;

@end

@implementation MoreGoodsController


- (void)initView{
    
    [super initView];
    [self.navBarView addTitle:@"更多优品"];
    [self.view addSubview:self.searchBgView];
    [self.view addSubview:self.selectView];
    [self.view addSubview:self.tableView];

    [self showHUD];
    self.salesNumSort = 0;
    self.priceSort = 0;
    self.selectIndex = 1;
    self.pageIndex = 1;
    [self loadData];
}

- (void)loadData{
    [self.dataModel getMoreGoodsDataWith:self.pageIndex salesNumSort:self.salesNumSort priceSort:self.priceSort productName:self.searchField.text categoryId:self.categoryId];
}

- (void)getDataFinish:(BaseModel *)baseModel netError:(NSError *)error{
    
    [self hideHUD];
    
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
//    [self.tableView.mj_footer setHidden:YES];
}

- (void)getDataFinish:(BaseModel *)baseModel netResponse:(BaseResponse *)response{
   
    [self hideHUD];

    MoreGoodsResponse *resp = (MoreGoodsResponse *)response;
    NSArray<MoreGoodsDataList> *list = resp.resObject.list;
    if (self.pageIndex == 1){
        [self.dataArray removeAllObjects];
    }
    [self.dataArray addObjectsFromArray:list];
    [self.tableView reloadData];

    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
    self.tableView.mj_header.hidden = !self.dataArray.count ;

    if(self.dataArray.count == 0 ){
        self.tableView.mj_header.hidden = YES ;
    }
}

- (void)headerRefresh
{
    self.pageIndex = 1 ;
    [self.dataArray removeAllObjects];
    [self loadData];
}

- (void)footerRefresh
{
    self.pageIndex++ ;
    [self loadData];
}

#pragma mark - tableviewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MoreGoodsCell *cell = [MoreGoodsCell cellWithTableView:tableView indexPath:indexPath];
    
    MoreGoodsDataList *model = self.dataArray[indexPath.row];
    cell.model = model;
    cell.index = indexPath.section ;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10.0;
}
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    return [[UIView alloc] init];
//}
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
//    return [[UIView alloc] init];
//}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MoreGoodsDataList *model = self.dataArray[indexPath.row];

    //商品详情模式：1默认模式，为空也默认 2三方链接
    if(model.shopProductDetailMode == 2){
        WKWebViewController *webController = [[WKWebViewController alloc]init];
        webController.titleString = @"详情";
        webController.urlString = model.thirdUrl;
        [self.navigationController pushViewController:webController animated:YES];
    }else{
        GoodsDetailController *detailVc = [[GoodsDetailController alloc]init];
        detailVc.productId = model.eid;
        [self.navigationController pushViewController:detailVc animated:YES];
    }
}

- (void)searchClick{
    self.pageIndex = 1;
    [self loadData];
}

- (void)defaultLabTap{
    if(self.selectIndex != 1){
        self.selectIndex = 1;
        self.salesNumSort = 0;
        self.priceSort = 0;
        [UIView animateWithDuration:0.5 animations:^{
            self.selectBottomline.left = 0;
        }];
        [self.tableView.mj_header beginRefreshing];
    }
}

- (void)salesViewTap{
    if(self.selectIndex != 2){
        self.selectIndex = 2;
        self.salesNumSort = 1;
        self.priceSort = 0;
        [UIView animateWithDuration:0.5 animations:^{
            self.selectBottomline.left = SCREEN_WIDTH/3;
        }];
        [self.tableView.mj_header beginRefreshing];
    }
}

- (void)priceViewTap{
    if(self.selectIndex != 3){
        self.selectIndex = 3;
        self.salesNumSort = 0;
        self.priceSort = 1;
        [UIView animateWithDuration:0.5 animations:^{
            self.selectBottomline.left = SCREEN_WIDTH/3*2;
        }];
        [self.tableView.mj_header beginRefreshing];
    }
}

- (UIView *)searchBgView{
    if(nil == _searchBgView){
        
        _searchBgView = [[UIView alloc]initWithFrame:CGRectMake(0, KNavFullHeight, SCREEN_WIDTH, 55)];
        _searchBgView.backgroundColor = [UIColor whiteColor];
        
        UIView *searchFieldBgView = [[UIView alloc]initWithFrame:CGRectMake(15, 15, _searchBgView.width-80, 30)];
        [searchFieldBgView setViewEdgeWithBorderWidth:1 cornerRadius:5 borderColor:kTitleColor_light];
        [_searchBgView addSubview:searchFieldBgView];
        
        UIImageView *searchImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 16, 16)];
        searchImage.image = [UIImage imageNamed:@"search_icon.png"];
        [searchFieldBgView addSubview:searchImage];
        
        UITextField *searchField =[[UITextField alloc] initWithFrame:CGRectMake(searchImage.right+10, 0, searchFieldBgView.width-searchImage.right-10, searchFieldBgView.height)];
        _searchField = searchField;
        searchField.delegate = self;
        searchField.font = KFontSmall;
        searchField.placeholder = @"  搜索商品";
        searchField.clearButtonMode = UITextFieldViewModeAlways;
        [searchFieldBgView addSubview:searchField];
        
        UILabel *searchLab = [[UILabel alloc] initWithFrame:CGRectMake(searchFieldBgView.right+5, 15, 50, 30)];
        searchLab.text = @"搜索";
        searchLab.textColor = kTitleColor_midden;
        searchLab.font = KFontSmall;
        searchLab.textAlignment = NSTextAlignmentCenter;
        [searchLab addTapCallBack:self sel:@selector(searchClick)];
        [_searchBgView addSubview:searchLab];
    }
    return  _searchBgView;
}

- (UITableView *)tableView
{
    if (nil == _tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _selectView.bottom+5, SCREEN_WIDTH ,SCREEN_HEIGHT-_selectView.bottom-10) style:UITableViewStylePlain];
        _tableView.dataSource = self ;
        _tableView.delegate = self ;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.showsVerticalScrollIndicator = NO ;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
        //        _tableView.mj_footer.hidden = YES ;
    }
    return _tableView ;
}

- (UIView *)selectView{
    if(nil == _selectView){
        _selectView = [[UIView alloc]initWithFrame:CGRectMake(0, _searchBgView.bottom+5, SCREEN_WIDTH, 42)];
        _selectView.backgroundColor = [UIColor whiteColor];
        
        CGFloat width = _selectView.width/3;
        
        UILabel *defaultLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 40)];
        defaultLab.text = @"默认";
        defaultLab.textColor = kTitleColor_midden;
        defaultLab.font = KFontSmall;
        defaultLab.textAlignment = NSTextAlignmentCenter;
        [defaultLab addTapCallBack:self sel:@selector(defaultLabTap)];
        [_selectView addSubview:defaultLab];
    
        UIView *salesView = [[UIView alloc]initWithFrame:CGRectMake(width, 0, width, 40)];
        [salesView addTapCallBack:self sel:@selector(salesViewTap)];
        [_selectView addSubview:salesView];
        
        UILabel *salesLab = [[UILabel alloc] initWithFrame:CGRectMake(width/2-23, 0, 30, salesView.height)];
        salesLab.text = @"销量";
        salesLab.textColor = kTitleColor_midden;
        salesLab.font = KFontSmall;
        [salesView addSubview:salesLab];
        
        UIImageView *sortImg = [[UIImageView alloc] initWithFrame:CGRectMake(salesLab.right, 12, 16, 16)];
        sortImg.image = kImage(@"shop_sort_unchoosed.png");
        [salesView addSubview:sortImg];
        
        UIView *priceView = [[UIView alloc]initWithFrame:CGRectMake(width*2, 0, width, 40)];
        [priceView addTapCallBack:self sel:@selector(priceViewTap)];
        [_selectView addSubview:priceView];
        
        UILabel *priceLab = [[UILabel alloc] initWithFrame:CGRectMake(width/2-23, 0, 30, priceView.height)];
        priceLab.text = @"价格";
        priceLab.textColor = kTitleColor_midden;
        priceLab.font = KFontSmall;
        [priceView addSubview:priceLab];
        
        UIImageView *sort_choose = [[UIImageView alloc] initWithFrame:CGRectMake(priceLab.right, 12, 16, 16)];
        sort_choose.image = kImage(@"shop_sort_unchoosed.png");
        [priceView addSubview:sort_choose];
        
        UIView * bottomline = [[UIView alloc] initWithFrame:CGRectMake(0, 40, width, 2)];
        bottomline.backgroundColor = UIColorFromRGB1(0xE5B580);
        _selectBottomline = bottomline;
        [_selectView addSubview:bottomline];
    }
    return _selectView;
}

- (MoreGoodsModel *)dataModel{

    if (nil == _dataModel) {
        _dataModel = [[MoreGoodsModel alloc]initWithDelegate:self cache:NO];
    }
    return _dataModel;
}

- (NSMutableArray *)dataArray{
    if(nil == _dataArray){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
@end
